package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Admin;
import com.baizhi.springboot.entity.Article;
import com.baizhi.springboot.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/selectArticleByPage")
    @ResponseBody
    public Map selectArticleByPage(Integer page, Integer limit) {
        return articleService.selectArticleByPage(page,limit);
    }
    @RequestMapping("/uploadArticle")
    @ResponseBody
    public Map  uploadArticle(MultipartFile file) throws IOException {
        file.transferTo(new File("D:/uploadFile/"+file.getOriginalFilename()));
        String articleImage=file.getOriginalFilename();
        System.out.println(articleImage);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("articleImage",articleImage);
        return map;
    }
    @RequestMapping("/insertArticle")
    @ResponseBody
    public Map  insertArticle(Article article) throws IOException {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            article.setArticleDate(new Date());
            //System.out.println(article+"===controer");
            articleService.insertArticle(article);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteArticle")
    @ResponseBody
    public Map  deleteArticle(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            articleService.deleteArticle(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneArticle")
    @ResponseBody
    public Map selectOneArticle(Integer id, HttpSession session) {
        Article article= articleService.selectOneArticle(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("article", article);
        return map;
    }

    @RequestMapping("/updateArticle")
    @ResponseBody
    public Map updateArticle(Article article) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            articleService.updateArticle(article);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
